<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap-4.0.0-dist/css/bootstrap.css">
</head>

<body style="display:flex;align-items: center;justify-content: center;height: 100vh;">
    <h1>
        <a href="index.html">首页</a>
    </h1>
    <form style="width:300px;border: 1px solid #CCC;
    padding: 20px;
    ">
        <div class="form-group">
            <label for="username">账号/邮箱</label>
            <input type="email" value="simba" class="form-control" id="username" aria-describedby="emailHelp"
                placeholder="请输入账号/邮箱">
            <small id="emailHelp" class="form-text text-muted">邮箱手机号等信息只会在本站内使用，大麦网承诺保护每位用户的隐私。</small>
        </div>
        <div class="form-group">
            <label for="pwd">密码</label>
            <input type="password" value="123123" class="form-control" id="pwd" placeholder="请输入密码">
        </div>
        <div class="form-check">
            <input type="checkbox" class="form-check-input" id="rememberme">
            <label class="form-check-label" for="rememberme">7天内免登录</label>
        </div>
        <button type="button" class="btn btn-primary">登录</button>
    </form>

    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="js/layer/layer.js"></script>
    <script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js"></script>
    <script>
        $(function () {
            $(".btn").click(function () {
                //prop() 常用于原生属性的获取
                //attr() 常用于自定义属性

                //加载层-风格3
                layer.load(2);
                //此处演示关闭
                setTimeout(function () {
                    layer.closeAll('loading');
                    var username = $("#username").val()
                    var pwd = $("#pwd").val()
                    var baseURL = `https://easy-mock.com/mock/5ceca555341d0c71bf7128f8`
                    $.post(baseURL + "/login", {
                        username,
                        pwd
                    }, data => {
                        if (data.state) {

                            if (!$("#rememberme").prop("checked")) {
                                $.cookie("username", username)
                            } else {
                                $.cookie("username", username, 60 * 24 * 7)
                            }

                            layer.msg("登录成功", {
                                icon: 6,
                                time: 500
                            }, function () {
                                location.href = "index.html"
                            })
                        } else {
                            layer.msg("用户名或密码错误", {
                                icon: 5
                            })
                        }
                    })
                }, 1000);

            })
        })
    </script>
</body>

</html>